package goodbaby.dklt.activity;

import android.os.Bundle;

import com.classic.core.activity.BaseSplashActivity;
import com.classic.core.utils.StringUtil;
import com.classic.core.utils.ToastUtil;
import com.classic.okhttp.gbb.http.ActionHelp;
import com.classic.okhttp.gbb.http.ObjectCallback;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

import goodbaby.dklt.GbbCache;
import goodbaby.dklt.GbbPreferences;
import goodbaby.dklt.R;
import goodbaby.dklt.bean.LoginInfo;

/**
 * Created by Jayden on 2016/4/19 16:36.
 *
 * @version V1.0
 * @Title: SplashActivity
 * @Description: 启动页
 */
public class SplashActivity extends BaseSplashActivity {

    private String account = "";
    private String isFirst = "y";
    private String isKeep = "n";
    private Class<?> nextActivity = LoginActivity.class;
    private String password = "";

    void checkLogin() {
        ActionHelp.userLogin(this, this.account, this.password, new ObjectCallback<LoginInfo>() {

            public void onError(int paramAnonymousInt) {
                ToastUtil.showToast(SplashActivity.this, "登陆失败");
                nextActivity = LoginActivity.class;
            }

            public void onError(int paramAnonymousInt, String paramAnonymousString) {
                super.onError(paramAnonymousInt, paramAnonymousString);
                ToastUtil.showToast(SplashActivity.this, "登陆失败");
                nextActivity = LoginActivity.class;
            }

            public void onHttpError(int paramAnonymousInt) {
                super.onHttpError(paramAnonymousInt);
                ToastUtil.showToast(SplashActivity.this, "网络异常");
                nextActivity = LoginActivity.class;
            }

            public void onSuccess(LoginInfo paramAnonymousLoginInfo) {
                GbbCache.setAccount(account);
                GbbCache.setPassword(password);
                GbbPreferences.saveUserAccount(account);
                GbbPreferences.saveUserPwd(password);
                GbbPreferences.saveBraceletAddress(paramAnonymousLoginInfo.getBraceletAddress());
                GbbPreferences.saveChildrenName(paramAnonymousLoginInfo.getChildrenName());
                GbbPreferences.saveSchoolName(paramAnonymousLoginInfo.getSchoolName());
                GbbPreferences.saveClassName(paramAnonymousLoginInfo.getClassName());
                if (!StringUtil.isEmpty(paramAnonymousLoginInfo.getSObject()))
                    GbbPreferences.saveSObject(paramAnonymousLoginInfo.getSObject());
                if (!StringUtil.isEmpty(paramAnonymousLoginInfo.getImagePath()))
                    GbbPreferences.saveHeadImg(paramAnonymousLoginInfo.getImagePath());
                if (!StringUtil.isEmpty(paramAnonymousLoginInfo.getNICKNAME()))
                    GbbPreferences.saveNickName(paramAnonymousLoginInfo.getNICKNAME());
                nextActivity = MainActivity.class;
            }

            @Override
            public Type getType() {
                return new TypeToken<LoginInfo>() {
                }.getType();
            }
        });
    }

    protected Class<?> nextActivity() {
        return nextActivity;
    }

    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        account = GbbPreferences.getUserAccount();
        password = GbbPreferences.getUserPwd();
        isKeep = GbbPreferences.getKeepLogin();
        if (isKeep != null && isKeep.equals("y")) {
            ToastUtil.showToast(SplashActivity.this, "正在登陆...");
            checkLogin();
        } else {
            nextActivity = LoginActivity.class;
        }
    }

    protected void setSplashResources(List<BaseSplashActivity.SplashImgResource> paramList) {
        isFirst = GbbPreferences.getIsFirst();
        GbbPreferences.saveIsFirst("n");
        SplashImgResource sr = new SplashImgResource();
        sr.setmResId(R.mipmap.start);
        sr.setPlayerTime(2000);
        paramList.add(sr);
        if (isFirst == null) {
            SplashImgResource sr2 = new SplashImgResource();
            sr2.setmResId(R.mipmap.welcome_1);
            sr2.setPlayerTime(3000);
            paramList.add(sr2);
            SplashImgResource sr3 = new SplashImgResource();
            sr3.setmResId(R.mipmap.welcome_2);
            sr3.setPlayerTime(3000);
            paramList.add(sr3);
            SplashImgResource sr4 = new SplashImgResource();
            sr4.setmResId(R.mipmap.welcome_3);
            sr4.setPlayerTime(3000);
            paramList.add(sr4);
            SplashImgResource sr5 = new SplashImgResource();
            sr5.setmResId(R.mipmap.welcome_4);
            sr5.setPlayerTime(3000);
            paramList.add(sr5);
        }
    }
}
